import axios from 'axios'
// import store from '@/store'
import { VueAxios } from './axios'
// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: '',
  timeout: 6000, // 请求超时时间
  headers: {
    'Content-Type':'application/json;application/x-www-form-urlencoded',
    // 'Content-Type':'application/json;charset=UTF-8'
  }
})
// 异常拦截处理器
const errorHandler = (error) => {
  if (error.response) {
    switch (error.response.status) {
      case 400:
          console.log('错误请求');
          break;
      case 401:
          console.log('请求需要验证，请登录');
          break;
      case 403:
        console.log('拒绝访问');
          break;
      case 404:
          console.log('请求错误,未找到该资源');
          break;
      case 500:
          console.log('服务器端出错');
          break;
      default:
          console.log('异常错误');
  }
  }
  return Promise.reject(error)
}

// 请求拦截
request.interceptors.request.use(config => {
    // 如果 token 存在
    // let token=""
    // if (token) {
    //     config.headers['Access-Token'] = token
    // }
    return config
}, errorHandler)

// 响应拦截
request.interceptors.response.use((response) => {
  return response.data
}, errorHandler)

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, request)
  }
}

export default request

export {
  installer as VueAxios,
  request as axios
}
